<?php


namespace App\Http\Controllers\Api;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Services\Business\Models\District;
use App\Http\Requests\ApiV2\DistrictRequest;
use App\Services\Member\Services\UserService;
use App\Services\Member\Interfaces\UserServiceInterface;

class DistrictController extends BaseController
{



    /**
     * @var UserService
     */
    protected $userService;
    protected $businessState;


    public function __construct(
        UserServiceInterface      $userService
    )
    {

        $this->userService = $userService;

    }


    /**
     *暂时通过名字来查询，同步socialite跟企业外部联系的数据
     */
    public function store(DistrictRequest $request, District $district)
    {


        $district->fill($request->filldata())->save();
        return $this->success(__('插入小区成功'));
    }

    public function delete(Request $request, $id)
    {

        Location::destroy($id);
        return $this->success(__('删除地址成功'));
    }

    public function update(DistrictRequest $request, $id)
    {

         $district = Location::query()->where('id', $id)->firstOrFail();
        $district->fill($request->filldata())->save();
        return $this->success(__('地址更新成功'));
    }



    public function detail($id)
    {

        $district = Location::query()
            ->where('id', $id)->firstOrFail();
        return $this->successData(compact('district'));
    }

    /**
     * 附近的小区
     * @param Request $request
     * @return JsonResponse
     */
    public function nearby(Request $request)
    {

        $currentLatitude =$request->input('latitude');
        $currentLongitude = $request->input('longitude');
        $range  =  0.01;
        $districts = Location::query()
            ->whereBetween('latitude', [$currentLatitude - $range, $currentLatitude + $range])
            ->whereBetween('longitude', [$currentLongitude - $range, $currentLongitude + $range])
            ->get();
        return $this->successData(compact('districts'));
    }


}
